package com.fullstack.landscape.editor.common;


/**
 * 返回结果
 * Created by thinkpad on 2017/6/6.
 */
public class Result<T> {

    /**
     *
     */
    private static final long serialVersionUID = 1122310481023540278L;

    private String code;

    private String msg;

    private T data;


    private Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private static <T> Result<T> success(String code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

    public static <T> Result<T> failure(String code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }


    /**
     * 成功
     */
    public static Result<Void> success() {
        String code = ResponseCode.SUCCESS.getCode();
        String msg = ResponseCode.SUCCESS.getMsg();
        return new Result<Void>(code, msg, null);
    }

    /**
     * 成功
     */
    public static <T> Result<T> success(T data) {
        String code = ResponseCode.SUCCESS.getCode();
        String msg = ResponseCode.SUCCESS.getMsg();
        return new Result<T>(code, msg, data);
    }

    /**
     * 成功+带响应消息
     *
     * @param responseCode 响应码
     */
    public static Result<Void> success(ResponseCode responseCode) {
        return success(responseCode.getCode(), responseCode.getMsg(), null);
    }

    /**
     * 成功+带响应消息+自定义消息
     *
     * @param responseCode 响应码
     * @param msg          自定义消息
     */
    public static Result<Void> success(ResponseCode responseCode, String msg) {
        return success(responseCode.getCode(), msg, null);
    }

    /**
     * 成功+带响应消息+自定义消息+数据
     *
     * @param responseCode 响应码
     * @param msg          自定义消息
     * @param data         数据
     */
    public static <T> Result<T> success(ResponseCode responseCode, String msg, T data) {
        return success(responseCode.getCode(), msg, data);
    }

    /**
     * 成功+带响应消息+数据
     *
     * @param responseCode 带响应消息
     * @param data         数据
     */
    public static <T> Result<T> success(ResponseCode responseCode, T data) {
        return success(responseCode.getCode(), responseCode.getMsg(), data);
    }

    /**
     * 失败+响应码
     *
     * @param responseCode 响应码
     */
    public static Result<Void> failure(ResponseCode responseCode) {
        return failure(responseCode.getCode(), responseCode.getMsg(), null);
    }

    /**
     * 失败+响应码+自定义消息
     *
     * @param responseCode 响应码
     */
    public static Result<Void> failure(ResponseCode responseCode, String msg) {
        return failure(responseCode.getCode(), msg, null);
    }

    /**
     * 失败+响应码+数据
     *
     * @param responseCode 响应码
     * @param data         数据
     */
    public static <T> Result<T> failure(ResponseCode responseCode, T data) {
        return failure(responseCode.getCode(), responseCode.getMsg(), data);
    }

    /**
     * 失败+响应码+数据+自定义消息
     *
     * @param responseCode 响应码
     * @param msg          自定义消息
     * @param data         数据
     */
    public static <T> Result<T> failure(ResponseCode responseCode, String msg, T data) {
        return failure(responseCode.getCode(), msg, data);
    }


    public T getData() {
        return data;
    }


    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}